package logic

import (
	"context"
	"fmt"
	"goodsrpc/cache"
	"goodsrpc/goods"
	"goodsrpc/internal/svc"
	"goodsrpc/pkg"
	"math/rand"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *goods.SendSmsRequest) (*goods.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	code := rand.Intn(9000) + 1000
	sms, _ := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	if *sms.Body.Message == "OK" {
		return nil, fmt.Errorf(*sms.Body.Code)
	}
	key := "send_sms" + in.Mobile
	cache.Rdb.Set(l.ctx, key, code, 0)

	return &goods.SendSmsResponse{}, nil
}
